var utils = require('./utils')
worker.onMessage(function (res) {
  console.log(">>>request-worker.onMessage",res)
  let requestIndex = res.requestIndex;
  // 速度
  let hSpeed = res.hSpeed;
  // 球滚动的路径数据
  let lightImgDatas = res.lightImgDatas;
  // 砖块形状数据
  let stepBlockDatas = res.stepBlockDatasMap;
  // 球的数据
  let ball = res.ball;
  // 屏幕宽度
  let stageWidth = res.stageWidth;
  //console.log(">>>111", stepBlockDatas);
  // 让砖块形状移动
  let needAddNewBlock = moveStepBlocks(stepBlockDatas, hSpeed, stageWidth);
  console.log(">>>222", stepBlockDatas);

  worker.postMessage({
    msg: 'worker-response-hello worker',
    responseIndex: requestIndex,
    stepBlockDatasMap: stepBlockDatas,
    needAddNewBlock: needAddNewBlock
  })
  
  /**
   * 让砖块及钻石移动
   */
  function moveStepBlocks(stepBlockDatas, hSpeed, stageWidth){
    let lastBlock = null;
    for (let key in stepBlockDatas){
      // 每一个砖块形状
      let stepBlock = stepBlockDatas[key];
      // 砖块形状上的砖块数组
      let stepsData = stepBlock["stepsData"];
      //console.log(">>>111-aaa", key, stepBlock, stepsData);
      moveSteps(stepBlock,stepsData,hSpeed);
      if (lastBlock == null || stepBlock.blockIndex > lastBlock.blockIndex){
        lastBlock = stepBlock;
      }
    }
    let flag = !lastBlock;
    let needAddNewBlock = false;
    //console.log(">>>lastBlock", lastBlock, !flag);
    if (!flag && lastBlock.lastStep.frame.x < stageWidth*1.5){
      needAddNewBlock = true;
    }
    return needAddNewBlock;
  }

  /**
   * 移动每一个砖块及上门的钻石
   */
  function moveSteps(stepBlock,stepsData, hSpeed){
    //console.log(">>>worker-moveSteps", stepBlock, stepsData,hSpeed);
    for (let key in stepsData){
      let stepData = stepsData[key];
      //console.log(">>>1111-key-value",key,stepData);
      //console.log(">>>111-bbb", key, stepsData, stepData);
      // 砖块移动
      stepData.frame.x = stepData.frame.x - hSpeed * 0.75;
      stepData.realX = stepData.realX - hSpeed * 0.75;
      // 速度变化
      let stepY = stepData.frame.y + hSpeed * 0.2;
      stepData.realY = stepData.realY + hSpeed * 0.2;
      
      // 让梯步进入可见范围再修正偏移量
      try{
        if (stepData.moveYDetla != 0 && stepData.frame.x < stepData.beginUpDownX) {
          // 初始移动推移时间变化
          if (stepData.moveYDelay > 0) {
            //console.log(">>>a");
            stepData.moveYDelay = stepData.moveYDelay - 1;
          }
          else if (stepData.moveYDetla > 10 && (stepData.moveYDetla - stepData.moveYSpeed) > 1) {
            // 如果是从上往下降
            // 先添加正常移动的y
            stepY += stepData.moveYSpeed;
            // 再添加往下降的y
            //console.log("***#b-1", stepData.moveYDetla, stepData.moveYSpeed);
            stepData.moveYDetla = stepData.moveYDetla - stepData.moveYSpeed;
            //console.log("***#b-2", stepData.moveYDetla, stepData.moveYSpeed);
          }
          else if (stepData.moveYDetla > 0) {
            //console.log(">>>c");
            // 从上往下接到快接近没有偏移时的位置时将偏移全部算完
            stepY += stepData.moveYDetla;
            // 偏移数据重置为0
            stepData.moveYDetla = 0;
            stepData.needPlayEffect = true;
            //SoundManager.playEffect(GameConfig.music.step2Center);
            //console.log("***c", stepData.moveYDetla);
          }
          else if (stepData.moveYDetla < -10 && (stepData.moveYDetla + stepData.moveYSpeed) < -1) {
            //console.log(">>>d");
            // 如果是从下往上升
            // 先添加正常移动的y
            stepY -= stepData.moveYSpeed;
            // 再添加往上升的y
            //console.log("***#d-1", stepData.moveYDetla, stepData.moveYSpeed);
            stepData.moveYDetla = stepData.moveYDetla + stepData.moveYSpeed;
            //console.log("***#d-2", stepData.moveYDetla, stepData.moveYSpeed);
          }
          else if (this.moveYDetla < 0) {
            //console.log(">>>e");
            // 从下往上升到快接近没有偏移时的位置时将偏移全部算完
            stepY -= stepData.moveYDetla;
            // 偏移数据重置为0
            stepData.moveYDetla = 0;
            stepData.needPlayEffect = true;
            //SoundManager.playEffect(GameConfig.music.step2Center);
            //console.log("***e", stepData.moveYDetla);
          }
        }
      }catch(e){
          console.log(">>>2222-moveYDetla-frame.x-beginUpDownX-moveYDelay", stepData.moveYDetla, stepData.frame.x, stepData.beginUpDownX, stepData.moveYDelay);
      }
      
      //console.log(">>>f", stepY);
      //console.log("***f", stepData.moveYDetla);
      stepData.frame.y = stepY;
      moveDiamond(stepData);

      if(stepData.isFirst){
        stepBlock.firstStep = stepData;
      }
      if (stepData.isLast) {
        stepBlock.lastStep = stepData;
      }
    }
    // 最后一个砖块看不见时删除形状
    if (stepBlock.lastStep.frame.x < -stepBlock.lastStep.frame.width){
      stepBlock.needRemove = true;
    }
    //console.log(">>> stepBlock", stepBlock, stepBlock.lastStep.frame.x);
  }
  function moveDiamond(stepData){
    //console.log(">>>111-ccc", stepData);
    // 钻石移动
    if (stepData.topDiamond) {
      // 让钻石水平位于砖块的中间
      stepData.topDiamond.frame.x = stepData.frame.x + (stepData.frame.width - stepData.topDiamond.frame.width) / 2;
      // 竖直位于上方向下偏移15的位置
      stepData.topDiamond.frame.y = stepData.frame.y - stepData.topDiamond.frame.height + 15;
    }
  }
})


